package 按位相加;

import 其他.DataStructure.ListNode;

public class 两个链表相加2 {
    public ListNode addTwoNumbers(ListNode h1, ListNode h2) {
        ListNode dm = new ListNode(-1);
        ListNode dmt = dm;

        int shang = 0;
        // 双指针 + w汉
        while(h1!=null && h2!=null){
            int sum = h1.val + h2.val + shang;
            int yu = sum % 10;
            dm.next = new ListNode(yu); // 老商
            shang = sum / 10 ; // 新cur商

            dm = dm.next;
            h1 = h1.next;
            h2 = h2.next;
        }
        if(h1 == null){
            while (h2!=null){
                int sum = h2.val + shang;
                dm.next = new ListNode(sum % 10);
                shang = sum / 10;

                dm = dm.next;
                h2 = h2.next;
            }
        }
        else if(h2 == null){
            while (h1!=null){
                int sum = h1.val + shang;
                dm.next = new ListNode(sum % 10);
                shang = sum / 10;

                dm = dm.next;
                h1 = h1.next;
            }
        }
        // 特 999 进1
        if(shang==1){
            dm.next = new ListNode(1);
        }
        return dmt.next;
    }
}
